<template>
	<view class="content">
		<view class="gradient-bg" >
			<!-- 渐变背景 -->
			<view class="s-input-wrap">
				<view class="s-input-box flex-row-between">
					<!-- <text class="black-text font-30"><text class="request">*</text>区域</text>
					<picker :value="area.dictLabel" @change="setArea" :range="areaList" range-key="dictLabel">
						<view class="black-text font-30">{{area && area.dictLabel?area.dictLabel:'请选择区域'}}</view>
					</picker> -->
					<text class="black-text font-30"><text class="request">*</text>线边仓</text>
					<picker :value="storehouse.no" @change="setStorehouse" :range="storehouseList" range-key="name">
						<view class="black-text font-30">{{storehouse && storehouse.name?storehouse.name:'请选择线边仓'}}</view>
					</picker>
				</view>
				<view class="s-input-box flex-row">
					<text class="black-text"><text class="request">*</text>物料编号</text>
					<input v-model="product.productNo" @confirm="setProductNo" class="s-input flex1" placeholder="扫描 / 输入物料" />
					<image v-if="product.productNo" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="clearProductNo"></image>
					<image class="s-input-icon" src="/static/images/scan.png"></image>
				</view>
				<view class="s-input-box flex-row-between">
					<text class="black-text font-30">物料名称</text>
					<text class="black-text font-30">{{product && product.productName?product.productName:'---'}}</text>
				</view>
				<view class="s-input-box flex-row">
					<text class="black-text"><text class="request">*</text>数量</text>
					<input @input="setNum" type="number" v-model="qty" class="s-input flex1" placeholder="请输入数量" />
					<image v-if="qty" class="s-input-icon clear-icon" src="/static/images/clear.png" @click="clearQty"></image>
				</view>
			</view>
				
			<view @click="addList" class="botton p-botton font-30" style="height: 92rpx; line-height:92rpx;margin: 0 25rpx;">
				加入申请单
			</view>
		</view>
		
		<view class="table-wrap" style="padding-bottom: 100rpx;">
			<view class="flex-row-start" style="margin: 10rpx 0;">
				<text class="gray-text font-24">申请单信息</text>
				<uni-icons type="top" size="14" style="color: #8997A8;"></uni-icons>
			</view>
			<zb-table :show-header="true" :columns="column" :fit="true" 
			@dele="deleteList" :border="true" :data="proStockApplyDetails">
			</zb-table>
		</view>
		
		<view class="content-list-total flex-row-between">
			<text class="gray-text">合计:</text>
			<view class="flex-row">
				<text class="gray-text">共<text class="blue-text font-38">{{total}}</text>件</text>
				<view class="botton p-botton" @click="addApply">完成</view>
			</view>
		</view>
			
	</view>
</template>

<script>
	import { validNum } from "@/utils/common"
	import {
		getStorehouse,
		getByProductNo
	} from "@/api/common.js"
	import {
		addProduct,
	} from "./api.js"
	
	const defaultList = []
	export default {
		data(){
			return{
				// area:{dictName:''},
				storehouseList:[],
				storehouse:{
					no:null,
					name:''
				},
				product:{
					productName:'',
					productNo:'',
				},
				qty: '',
				proStockApplyDetails:[],
				// areaList:[],
				tableHeiht: 0,
				column: [
					{ type:'index', label: '序号', align: 'center', width:56 },
					{
						name: 'productNo',
						label: '物料编号',
						// emptyString: '--'
					},
					{
						name: 'productName',
						label: '物料名称',
					},
					{
						name: 'orderQty',
						label: '数量',
						width: 56
					},
					{
						name: 'operation',
						type: 'operation',
						label: '操作',
						renders: [
							{
								name: '/static/images/delete.png',
								type: 'icon',
								func:"dele",
								style:"width:24rpx;height:28rpx"
							},
						],
						width: 56
					},
				],
			}
		},
		computed: {
			total:function(){
				let qty = 0
				this.proStockApplyDetails.forEach(item=>{
					qty += parseFloat(item.orderQty) 
				})
				return qty || 0
			},
		},
		onLoad(){
			this.tableHeiht = this.windowHeight - 420;
			// 获取区域
			// this.getDicts("basic_area").then(res => {
			//     this.areaList = res.data;
			// });
			this.getListStorehouse();
		},
		methods:{
			// 获取仓库数据
			getListStorehouse() {
			  getStorehouse({ isLine: "Y", pageNum: 1, pageSize: 0 }).then((res) => {
				this.storehouseList = res.rows
			  })
			},
			// 数量校验
			setNum(e){
				this.$set(this,'qty',validNum(e))
			},
			// // 选择区域
			// setArea(e) {
			// 	this.area = this.areaList[e.detail.value]
			// },
			// 选择仓库
			setStorehouse(e) {
				this.storehouse = this.storehouseList[e.detail.value]
			},
			// 选择物料 获取物料信息
			setProductNo(event){
				getByProductNo(event.detail.value).then(res=>{
					if(res.code == 200 && res.data){
						this.product = res.data
					}else{
						this.$modal.msg('此物料存在异常')
						this.$set(this,'product',{productNo:'',productName:''})
					}
				})
			},
			// 添加申请明细记录
			addList(){
				if(!(this.storehouse && this.storehouse.no)){
					this.$modal.msg('请选择仓库')
					return
				}
				if(!this.product.productNo){
					this.$modal.msg('请扫描/输入物料编号')
					return
				}
				if(!this.qty){
					this.$modal.msg('请输入数量')
					return
				}
				let isExists = false
				for(let i=0;i<this.proStockApplyDetails.length;i++){
					let item = this.proStockApplyDetails[i];
					if(item.productNo==this.product.productNo){
						isExists = true;
						this.$modal.msg(`已存在该物料数据，请直接更改数量`);
						this.$set(this,'product',{productNo:'',productName:''})
						return
					}
				}
				if(isExists) return;
				this.proStockApplyDetails.push({
					productName:this.product.productName,
					productNo:this.product.productNo,
					orderQty: this.qty,
				})
				this.$set(this,'product',{productNo:'',productName:''})
				this.$set(this,'qty','')
			},
			deleteList(item){
				this.proStockApplyDetails.splice(item.key,1);
			},
			cleanForm(){
				// this.$set(this,'area',{dictName:''})
				this.$set(this,'storehouse',{no:null,name:''})
				this.$set(this,'product',{productNo:null,productName:''})
				this.$set(this,'qty','')
				this.$set(this,'proStockApplyDetails',[])
			},
			addApply(){
				if(!(this.storehouse && this.storehouse.no)){
					this.$modal.msg('请选择仓库')
					return
				}
				if(!this.proStockApplyDetails.length){
					this.$modal.msg('请输入物料')
					return
				}
				this.$modal.confirm('是否完成申请？').then(() => {
					this.$modal.loading("提交申请中...")
					addProduct({
						storehouseNo: this.storehouse.no,
						storehouseName: this.storehouse.name,
						proStockApplyDetails: this.proStockApplyDetails
					}).then(res=>{
						this.$modal.closeLoading()
						if(res.code == 200){
							this.$modal.msgSuccess("操作成功");
							this.cleanForm()
						}
					})
				})
			},
			clearProductNo() {
				this.$set(this, 'product', { productNo: '', productName: '' })
			},
			clearQty() {
				this.$set(this, 'qty', '')
			}
		}
	}
</script>

<style>
	page{
		background-color: #F8F8F8;
	}
</style>
<style scoped>
	
</style>